#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "video.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("GUI_photo_video");
    QSplitter *spm = new QSplitter(Qt::Horizontal, this);

    // 创建树形结构和其他部件
    treeView = new QTreeView(spm);
    model = new QStandardItemModel(treeView);
    Filedialog = new QFileDialog;
    treeView->setModel(model);
    QStandardItem *rootItem = model->invisibleRootItem();
    treeView->setHeaderHidden(true);
    QStandardItem *item1 = new QStandardItem("图片播放");
    item1->setEditable(true);
    QStandardItem *item2 = new QStandardItem("视频播放");
    item2->setEditable(false);
    rootItem->appendRow(item1);
    rootItem->appendRow(item2);
    rootPath_Image = "D:/photo/";
    rootPath_Video = "D://video//";
    addImageItemsRecursively(item1, rootPath_Image);
    addVideoItemsRecursively(item2, rootPath_Video);
    treeView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    treeView->expandAll();
    QDockWidget *dock = new QDockWidget;
    dock->setWidget(treeView);
    this->addDockWidget(Qt::LeftDockWidgetArea, dock);

    //right splitter
    spRight = new QSplitter(Qt::Vertical, spm);
    spRight->setOpaqueResize(true);
    spRight->setStretchFactor(1,1);
    //video widget
    videoWidget = new QWidget(spRight);
    videoWidget->setStyleSheet("QWidget {background-color: white}");
    QHBoxLayout *videoLayout = new QHBoxLayout(videoWidget);

    //设置字体样式
    font=new QFont;
    font->setPointSize(18);
    font->setBold(true);
    font->setFamily("SimSun");

    //image widget
    imageWidget = new QWidget(spRight);
    imageWidget->setStyleSheet("QWidget {background-color: white}");
    QHBoxLayout *imageLayout = new QHBoxLayout(imageWidget);

    imageLabel = new QLabel(imageWidget);
    imageLabel->setAlignment(Qt::AlignCenter);
    imageLabel->setFont(*font);
    imageLabel->setText("Image");

    imageLayout->addWidget(imageLabel);
    imageWidget->setLayout(imageLayout);
    imageLayout->setContentsMargins(0,0,0,0);

    test = new QLabel();
    videoLayout->addWidget(test);
    videoWidget->setLayout(videoLayout);
    videoLayout->setContentsMargins(0,0,0,0);

    //    imageLabel->setScaledContents(true);

    //    spRight->addWidget(imageWidget);
    spm->setStretchFactor(1, 2);
    // 设置中央部件
    this->setCentralWidget(spm);

    my_videoWidget = new video(NULL);
    //设置视频窗口样式
    my_videoWidget->setWindowFlags(Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint | Qt::WindowStaysOnTopHint);
        my_videoWidget->setFixedSize(610,310);
        QSize layoutSizeHint = my_videoWidget->size();
        qDebug()<<layoutSizeHint;
    //设置菜单栏
    auto menu = menuBar();
    auto filemenu=menu->addMenu("文件(&F)");
    auto editmenu=menu->addMenu("编辑(&E)");
    auto viewmenu = menu->addMenu("视图(&V)");
    auto helpwmenu = menu->addMenu("帮助(&H)");
    filemenu->addAction(QIcon("D:/image.png"),"打开图片");
    filemenu->addAction(QIcon("D:/video.png"),"打开视频");
    //设置侧边工具栏
    toolBar = new QToolBar("1",this);
    addToolBar(Qt::LeftToolBarArea,toolBar);
    QAction* open_image = new QAction(QIcon(":/icon/image.png"), tr("打开图片"), this);
    toolBar->addAction(open_image);
    toolBar->addSeparator();
    QAction* open_video = new QAction(QIcon(":/icon/video.png"), tr("打开图片"), this);
    toolBar->addAction(open_video);
    toolBar->addSeparator();



    // 连接树结构的点击信号和槽函数
    connect(treeView, SIGNAL(clicked(QModelIndex)),this,SLOT(slot_clickTree(QModelIndex)));
    connect(filemenu->actions().at(0), &QAction::triggered, this, &MainWindow::openImage);
    connect(filemenu->actions().at(1), &QAction::triggered, this, &MainWindow::openVideo);
    connect(open_image, &QAction::triggered, this, &MainWindow::openImage);
    connect(open_video, &QAction::triggered, this, &MainWindow::openVideo);

}

void MainWindow::slot_clickTree(QModelIndex index) {
    // 获取被点击的树节点
    QStandardItem *clickedItem = model->itemFromIndex(index);
    QString clickedItemText = clickedItem->text();
    QString image_path=QString("%1%2").arg(rootPath_Image).arg(clickedItemText);
    QString video_path=QString("%1%2").arg(rootPath_Video).arg(clickedItemText);
    QFileInfo fileInfo_image(image_path);
    QFileInfo fileInfo_video(video_path);


    // 如果节点文本不为空且为文件，则将图片显示在窗口中
    if(!clickedItemText.isEmpty() && QFileInfo(image_path).isFile())
    {
        QPixmap pixmap(image_path);
        double num_1 = double(pixmap.size().width())/double(pixmap.size().height());
        double scaleWidth = imageWidget->size().height()*num_1;
        double widgetHeight = imageWidget->size().height();
        double widgetWidth= imageWidget->size().width();

        if(scaleWidth > imageWidget->size().width())
            pixmap = pixmap.scaled(widgetWidth,widgetWidth/num_1,Qt::KeepAspectRatio);
        else
            pixmap = pixmap.scaled(widgetHeight*num_1,widgetHeight,Qt::KeepAspectRatio);

        imageLabel->setPixmap(pixmap);

        //将点击的图片信息显示在另一个label中
        QDateTime createdTime = fileInfo_image.created();
        //        QString fileName = fileInfo.fileName();
        qint64 fileSize = fileInfo_image.size();
        QSize imageSize = pixmap.size();
        int imageWidth = pixmap.width();
        int imageHeight = pixmap.height();
        //        QString imageType = fileInfo.suffix();
        QString createdTimeStr = "创建时间:" + createdTime.toString(Qt::ISODate);
        QString fileName = "图片名称:" + fileInfo_image.fileName();
        QString fileSizeStr = "图片大小:" + QString::number(fileSize) + "字节";
        QString imageSizeStr = "图片分辨率为:" + QString::number(imageWidth) + "x" + QString::number(imageHeight);
        QString imageType = "图片类型:" + fileInfo_image.suffix();
        QString infoStr = createdTimeStr + "\n" +
                fileName + "\n" +
                fileSizeStr + "\n" +
                imageSizeStr + "\n" +
                imageType;
        test->setAlignment(Qt::AlignTop | Qt::AlignLeft);
        test->setFont(*font);
        test->setText(infoStr);
    }
    //如果点击视频文件则另开一个窗口显示
    if(!clickedItemText.isEmpty() && QFileInfo(video_path).isFile())
    {
        qDebug()<<video_path;
        my_videoWidget->setVideoUrl(video_path);
        my_videoWidget->setWindowTitle(clickedItemText);
        my_videoWidget->show();
//        QSize videoSize = my_videoWidget->player->videoSize();
        //将点击视频信息显示在另一个label中
        QDateTime createdTime = fileInfo_video.created();
        //        QString fileName = fileInfo.fileName();
        qint64 fileSize = fileInfo_video.size();

        //        QString imageType = fileInfo.suffix();
        QString createdTimeStr = "创建时间:" + createdTime.toString(Qt::ISODate);
        QString fileName = "视频名称:" + fileInfo_video.fileName();
        QString fileSizeStr = "视频大小:" + QString::number(fileSize) + "字节";
        QString imageType = "视频类型:" + fileInfo_video.suffix();
        QString infoStr = createdTimeStr + "\n" +
                fileName + "\n" +
                fileSizeStr + "\n" +
                imageType;
        test->setAlignment(Qt::AlignTop | Qt::AlignLeft);
        test->setFont(*font);
        test->setText(infoStr);
        //每点击一个视频开一个窗口
        //        video *vide_show = new video;
        //        vide_show->setWindowTitle(clickedItemText);

        //        QUrl videoUrl(video_path);
        //        vide_show->player->setMedia(videoUrl);
        //        vide_show->show();
        //        vide_show->player->play();
    }
}
//void MainWindow::resizeEvent(QResizeEvent *event) {
//    QMainWindow::resizeEvent(event);

//    // 获取当前被点击的树节点
//    QModelIndex currentIndex = treeView->currentIndex();
//    QStandardItem *clickedItem = model->itemFromIndex(currentIndex);
//    QString clickedItemText = clickedItem->text();
//    QString image_path = QString("%1%2").arg(rootPath_Image).arg(clickedItemText);
//    // 如果节点文本不为空且为文件，则加载图片到 QLabel 中
//    if (!clickedItemText.isEmpty() && QFileInfo(image_path).isFile()) {
//        QPixmap pixmap(image_path);

//        // 缩放图片以适应右下部分的大小
//        QSize imageSize = spRight->size(); // 获取右下部分的大小
//        pixmap = pixmap.scaled(imageSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);

//        // 设置图片到 imageLabel 中
//        imageLabel->setPixmap(pixmap);
//    }
//}


void MainWindow:: addImageItemsRecursively(QStandardItem *parentItem, const QString &path) {
    QDir dir(path);
    QFileInfoList fileList = dir.entryInfoList(QStringList() << "*.png" << "*.jpg", QDir::Files);

    foreach (const QFileInfo &fileInfo, fileList) {
        QStandardItem *item = new QStandardItem(fileInfo.fileName());
        QPixmap pixmap(fileInfo.filePath());
        item->setIcon(QIcon(pixmap));
        item->setEditable(false);
        item->setWhatsThis(fileInfo.filePath());
        parentItem->appendRow(item);
    }

}
void MainWindow:: addVideoItemsRecursively(QStandardItem *parentItem, const QString &path) {
    QDir dir(path);
    QFileInfoList fileList = dir.entryInfoList(QStringList() << "*.mp4" << "*.mp5", QDir::Files);

    foreach (const QFileInfo &fileInfo, fileList) {
        QStandardItem *item = new QStandardItem(fileInfo.fileName());
        item->setEditable(false);
        item->setWhatsThis(fileInfo.filePath());
        parentItem->appendRow(item);
    }

}
void MainWindow::openImage()
{
    QString filename;
    filename=Filedialog->getOpenFileName(nullptr,"选择图片文件");
    QPixmap pixmap(filename);
    QFileInfo fileInfo(filename);
    double num_1 = double(pixmap.size().width())/double(pixmap.size().height());
    double scaleWidth = imageWidget->size().height()*num_1;
    double widgetHeight = imageWidget->size().height();
    double widgetWidth= imageWidget->size().width();

    if(scaleWidth > imageWidget->size().width())
        pixmap = pixmap.scaled(widgetWidth,widgetWidth/num_1,Qt::KeepAspectRatio);
    else
        pixmap = pixmap.scaled(widgetHeight*num_1,widgetHeight,Qt::KeepAspectRatio);

    imageLabel->setPixmap(pixmap);
    //将点击的图片信息显示在另一个label中
    QDateTime createdTime = fileInfo.created();
    //        QString fileName = fileInfo.fileName();
    qint64 fileSize = fileInfo.size();
    QSize imageSize = pixmap.size();
    int imageWidth = pixmap.width();
    int imageHeight = pixmap.height();
    //        QString imageType = fileInfo.suffix();
    QString createdTimeStr = "创建时间:" + createdTime.toString(Qt::ISODate);
    QString fileName = "图片名称:" + fileInfo.fileName();
    QString fileSizeStr = "图片大小:" + QString::number(fileSize) + "字节";
    QString imageSizeStr = "图片分辨率为:" + QString::number(imageWidth) + "x" + QString::number(imageHeight);
    QString imageType = "图片类型:" + fileInfo.suffix();
    QString infoStr = createdTimeStr + "\n" +
            fileName + "\n" +
            fileSizeStr + "\n" +
            imageSizeStr + "\n" +
            imageType;
    test->setAlignment(Qt::AlignTop | Qt::AlignLeft);
    test->setFont(*font);
    test->setText(infoStr);

}
void MainWindow::openVideo()
{

//    my_videoWidget->insertvideo();
    QString filename;
    filename=Filedialog->getOpenFileName(nullptr,"选择视频文件");
    if(filename != "")
    {
        //    filename = "rtsp://localhost:8554/aaa";
        QUrl videoUrl(filename);
        my_videoWidget->player->setMedia(videoUrl);
        my_videoWidget->player->play();
    }
    my_videoWidget->show();
    QFileInfo fileInfo_video(filename);
    QDateTime createdTime = fileInfo_video.created();
    //        QString fileName = fileInfo.fileName();
    qint64 fileSize = fileInfo_video.size();

    //        QString imageType = fileInfo.suffix();
    QString createdTimeStr = "创建时间:" + createdTime.toString(Qt::ISODate);
    QString fileName = "视频名称:" + fileInfo_video.fileName();
    QString fileSizeStr = "视频大小:" + QString::number(fileSize) + "字节";
    QString videoType = "视频类型:" + fileInfo_video.suffix();
//    my_videoWidget->player->
    QString infoStr = createdTimeStr + "\n" +
            fileName + "\n" +
            fileSizeStr + "\n" +
            videoType;
    test->setAlignment(Qt::AlignTop | Qt::AlignLeft);
    test->setFont(*font);
    test->setText(infoStr);
}

MainWindow::~MainWindow()
{
    delete my_videoWidget;
    delete ui;
}
